package com.fhnew.app.ui.activity.userinfo.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.fhnew.app.R;
import com.fhnew.app.model.internet.HttpAPI;
import com.fhnew.app.model.internet.HttpRequest;
import com.fhnew.app.model.internet.been.B_YuEMain;
import com.fhnew.app.model.internet.model.UserModel;
import com.fhnew.app.ui.activity.userinfo.FragmentDataChang;
import com.fhnew.app.ui.activity.userinfo.PageDataChangeControl;
import com.fhnew.app.util.DateUtil;
import com.fhnew.app.util.StringUtil;
import com.fhnew.app.widget.LoadingDialog;

import java.util.Calendar;
import java.util.Date;

/**
 * 余额宝首页
 */
public class YuEFragment extends Fragment implements FragmentDataChang {

    private PageDataChangeControl pageDataChangeControl;

    public void setListener (PageDataChangeControl pageDataChangeControl) {
        this.pageDataChangeControl = pageDataChangeControl;
    }

    @Override
    public void dataChanged (Bundle bundle) {

    }

    //=================================================================
    //View
    //=================================================================
    private TextView count01,count02,count03,count04;
    private View press01, press02, press03, press04;
    private Button press11, press12, press13;
    private TextView press21, press22;
    private LoadingDialog loadingDialog;
    private TextView time;

    private void initView (View view) {
        loadingDialog = new LoadingDialog((ViewGroup) view);
        loadingDialog.setDialogCancelable(false);
        loadingDialog.setShowTips(false);

        time = view.findViewById(R.id.textView_YuE_Main_Time);

        count01 = view .findViewById(R.id.textView_YuE_Main_011);
        count02 = view .findViewById(R.id.textView_YuE_Main_021);
        count03 = view .findViewById(R.id.textView_YuE_Main_031);
        count04 = view .findViewById(R.id.textView_YuE_Main_041);

        press01 = view.findViewById(R.id.view_YuE_Main_01);
        press02 = view.findViewById(R.id.view_YuE_Main_02);
        press03 = view.findViewById(R.id.view_YuE_Main_03);
        press04 = view.findViewById(R.id.view_YuE_Main_04);

        press01.setOnClickListener(onClickListener);
        press02.setOnClickListener(onClickListener);
        press03.setOnClickListener(onClickListener);
        press04.setOnClickListener(onClickListener);

        press11 = view.findViewById(R.id.button_YuE_Main_Charge01);
        press12 = view.findViewById(R.id.button_YuE_Main_Charge02);
        press13 = view.findViewById(R.id.button_YuE_Main_Charge03);

        press11.setOnClickListener(onClickListener);
        press12.setOnClickListener(onClickListener);
        press13.setOnClickListener(onClickListener);

        press21 = view.findViewById(R.id.textView_YuE_Main_Help01);
        press22 = view.findViewById(R.id.textView_YuE_Main_Help02);

        press21.setOnClickListener(onClickListener);
        press22.setOnClickListener(onClickListener);

    }

    private void initInfoView(){
        if(b_yuEMain != null){
            B_YuEMain.DataBean dataBean = b_yuEMain.getData();

            count01.setText("￥ "+dataBean.getUserAccountMoney());
            count02.setText("￥ "+dataBean.getUserAllIncome());
            count03.setText("￥ "+dataBean.getUserYesDayIncome());
            count04.setText("￥ "+dataBean.getClaimProportion());

            StringUtil.format(count01);
            StringUtil.format(count02);
            StringUtil.format(count03);
//          StringUtil.format(count04);

            Date date = DateUtil.getDate( b_yuEMain.getServerTime(),DateUtil.Format01);
            Calendar calendar =Calendar.getInstance();
            calendar.setTime(date);
            second = 60 - calendar.get(Calendar.SECOND);
            refreshTime();
            handler.sendEmptyMessage(0);
        }
    }

    private void refreshTime(){
        time.setText(""+second);
    }

    //=================================================================
    //
    //=================================================================
    @Nullable
    @Override
    public View onCreateView (@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        container = (ViewGroup) inflater.inflate(R.layout.fragment_yue, null);
        initView(container);
        initInfo();
        return container;
    }

    @Override
    public void onResume () {
        super.onResume();
        initInfoView();
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage (Message msg) {
            super.handleMessage(msg);

            if(second>0){
                refreshTime();
                second--;
                handler.sendMessageDelayed(Message.obtain(),1000);
            }else{
                getYuE.startRequest(userModel.getToken());
            }
        }
    };

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick (View v) {
            int id = v.getId();
            Bundle bundle = new Bundle();

            switch (id) {
                case R.id.view_YuE_Main_01:
                    bundle.putString("conentTag","1");
                    pageDataChangeControl.changeFragmemt("CashList",bundle);
                    break;
                case R.id.view_YuE_Main_02:
                    bundle.putString("conentTag","2");
                    pageDataChangeControl.changeFragmemt("CashList",bundle);
                    break;
                case R.id.view_YuE_Main_03:
                    bundle.putString("conentTag","3");
                    pageDataChangeControl.changeFragmemt("CashList",bundle);
                    break;
                case R.id.view_YuE_Main_04:
                    bundle.putString("conentTag","4");
                    pageDataChangeControl.changeFragmemt("CashList",bundle);
                    break;
                case R.id.button_YuE_Main_Charge01:
                    if(userModel.getUser().isHasFundPwd()){
                        bundle.putString("conentTag","1");
                        pageDataChangeControl.changeFragmemt("Charge",bundle);
                    }else{
                        Toast.makeText(getContext(), "请先设置支付密码", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case R.id.button_YuE_Main_Charge02:
                    if(userModel.getUser().isHasFundPwd()){
                        bundle.putString("conentTag","2");
                        pageDataChangeControl.changeFragmemt("Charge",bundle);
                    }else{
                        Toast.makeText(getContext(), "请先设置支付密码", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case R.id.button_YuE_Main_Charge03:
                    bundle.putString("conentTag","3");
                    pageDataChangeControl.changeFragmemt("Charge",bundle);
                    break;
                case R.id.textView_YuE_Main_Help01:
                    bundle.putString("conentTag","1");
                    bundle.putString("content",b_yuEMain.getUnderstand());
                    pageDataChangeControl.changeFragmemt("Help",bundle);
                    break;
                case R.id.textView_YuE_Main_Help02:
                    bundle.putString("conentTag","2");
                    bundle.putString("content",b_yuEMain.getProblem());
                    pageDataChangeControl.changeFragmemt("Help",bundle);
                    break;
            }
        }
    };


    private HttpAPI.GetYuE getYuE = new HttpAPI.GetYuE() {

        @Override
        public void onStartedRequest (HttpRequest httpRequest) {
            super.onStartedRequest(httpRequest);
            loadingDialog.showLoading();
        }

        @Override
        public void result (B_YuEMain result) {
            if(result.getCode() == 600){
                pageDataChangeControl.changeFragmemt("finish",null);
                return;
            }
            b_yuEMain = result;
            initInfoView();
            loadingDialog.dismiss();
        }

        @Override
        public void error (String msg) {
            loadingDialog.dismiss();
            Toast.makeText(getContext(), "余额信息获取失败", Toast.LENGTH_SHORT).show();
        }

    };


    private UserModel userModel;
    private B_YuEMain b_yuEMain;
    private int second;

    private void initInfo(){
        userModel = UserModel.getInstance(getContext());
        getYuE.startRequest(userModel.getToken());
    }


}
